import {
  RECEIVE_USER,
  RECEIVE_SINGLE_USER,
  RECEIVE_NEWS,
  RECEIVE_SINGLE_NEWS,
  RECEIVE_TOPIC,
  RECEIVE_TODAY_NEWS,
  RECEIVE_SIDEBAR,
  RECEIVE_LOGIN_INFO,
  RECEIVE_COMMENTS,
  RECEIVE_NEWS_COMMENTS,
  RECEIVE_VERIFICATION_CODE
} from "./mutation-types"

export default {
  [RECEIVE_USER](state, {user}) {
    state.user = user
  },
  [RECEIVE_SINGLE_USER](state, singleUser) {
    state.singleUser = singleUser
  },
  [RECEIVE_NEWS](state, {news}) {
    state.news = news
  },
  [RECEIVE_SINGLE_NEWS](state, singleNews) {
    state.singleNews = singleNews
  },
  [RECEIVE_TOPIC](state, topic) {
    state.topic = topic
  },
  [RECEIVE_TODAY_NEWS](state, todayNews) {
    state.todayNews = todayNews
  },
  [RECEIVE_SIDEBAR](state, sidebar) {
    state.sidebar = sidebar
  },
  [RECEIVE_LOGIN_INFO](state, user_info) {
    window.sessionStorage.setItem('token', user_info.token)
    window.sessionStorage.setItem('userInfo', JSON.stringify(user_info.user))
    if (user_info.adminToken) {
      window.sessionStorage.setItem('adminToken', user_info.adminToken)
    }
  },
  [RECEIVE_COMMENTS](state, comments) {
    state.comments = comments
  },
  [RECEIVE_NEWS_COMMENTS](state, newsComments) {
    state.newsComments = newsComments.reverse()
  },
  [RECEIVE_VERIFICATION_CODE](state, verificationCode) {
    state.verificationCode = verificationCode
  }
}